<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="parentPermissionList.name"
                     @change="selectChange"
                     placeholder="请选择"
                     clearable
          >
            <el-option
                v-for="item in parentPermissionList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="permissionList.rows"
              style="width: 100%;">
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="权限名" width="150">
      </el-table-column>
      <el-table-column prop="sn" label="SN" width="300">
      </el-table-column>
      <el-table-column prop="url" label="URL" width="300">
      </el-table-column>
      <el-table-column prop="parent.name" label="一级权限" width="150">
      </el-table-column>
      <el-table-column prop="description" label="简介" >
      </el-table-column>
    </el-table>

<!--    工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="permissionList.total">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
export default {
  mounted() {
    this.getPermission()
    this.getParent()
  },
  data(){
    return{
      query:{
        keyword:"",
        parentId:null,
        currentPage:1,
        pageSize:10
      },
      permissionList:{
        rows:[],
        total:0
      },
      parentPermissionList:[]
    }
  },
  methods:{
    selectChange(val){
      this.query.parentId = val
    },
    getParent() {
      this.$http.get("/permission")
      .then(result => {
        result = result.data
        this.parentPermissionList = result.data
      })
      .catch(() => {
        this.$message({
          message: '网络异常',
          type: 'error'
        });
      })
    },
    search(){
      this.query.currentPage = 1
      this.getPermission()
    },
    handleSizeChange(val){
      this.query.pageSize = val;
      this.query.currentPage = 1;
      this.getPermission()
    },
    handleCurrentChange(val){
      this.query.currentPage = val
      this.getPermission()
    },
    getPermission(){
      this.$http.post("/permission",this.query)
      .then(result =>{
        result = result.data
        if (result.success){
          this.permissionList = result.data
        } else{
          this.$message({
            message: result.data.message,
            type: 'error'
          });
        }

      })
      .catch(() =>{
        this.$message({
          message: '网络异常',
          type: 'error'
        });
      })
    }
  }
}
</script>

<style scoped>

</style>